package com.android.xunxian.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.adapter.DeviceDeilstAdpter;
import com.android.xunxian.entry.DiviceDeilst;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2016/8/7.
 */
public class DeviceZBActivity extends Activity implements View.OnClickListener{

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.listview)
    ListView listview;
    String deviceName;
    private DeviceDeilstAdpter adpter;
    private List<DiviceDeilst> diviceDeilsts;
    ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_zb_activity);
        ButterKnife.inject(this);
        close.setOnClickListener(this);
        deviceName=getIntent().getStringExtra("dn");
        titleText.setText(deviceName+"指标");
        diviceDeilsts=new ArrayList<>();
        adpter=new DeviceDeilstAdpter(this,diviceDeilsts);
        listview.setAdapter(adpter);
        getDate(deviceName);

    }

    //获取设备指标列表
    private void getDate(String devid) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.ZB_LIST);
        params.addBodyParameter("LKind", "CBJ");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("DEVID", devid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {


                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<DiviceDeilst>>() {
                            }.getType();
                            List<DiviceDeilst> list = gson.fromJson(result, lt);
//
                            if (list != null && list.size() > 0) {
//
                                diviceDeilsts.addAll(list);
                                adpter.notifyDataSetChanged();


                            } else {
                                Toast.makeText(x.app(), "暂无设备！", Toast.LENGTH_LONG).show();
                            }


                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
        }
    }
}
